<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <div class="shuxing">
       <el-form-item label="行政区域" prop="areaId">
              <treeselect class="shuxing1" v-model="queryParams.enterArea" :options="areaList" :normalizer="normalizer" placeholder="选择区域" />
        </el-form-item>
        </div>
       <el-form-item label="主体名称" prop="enterName">
        <el-input
          v-model="queryParams.enterName"
          placeholder="请输入主体名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
        <el-form-item label="所属行业" prop="industry">
        <el-select
                v-model="queryParams.industry"
                clearable
                placeholder="请选择"
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['TProductCertificationInfo:certificationInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['TProductCertificationInfo:certificationInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['TProductCertificationInfo:certificationInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['TProductCertificationInfo:certificationInfo:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="certificationInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="主体名称" align="center" prop="enterName" />
      <el-table-column label="所属行业" align="center" prop="industry" />
      <el-table-column label="产品分类" align="center" prop="productClassify" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="创建时间" align="center" prop="certificateTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['TProductCertificationInfo:certificationInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['TProductCertificationInfo:certificationInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改农产品认证对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="主体名称" prop="enterId">
         <!--  <el-input v-model="form.enterId" placeholder="请输入主题编号" /> -->
          <el-select style="width: 580px;"
                v-model="form.enterId"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in EnterpriseInfoList"
                  :key="item.id"
                  :label="item.enterName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
        </el-form-item>
        <el-form-item label="农产品认证类别">
          <el-select v-model="form.certificateType" placeholder="请选择认证类别" style="width: 580px;">
            <el-option label="无公害产品" value="无公害产品" />
               <el-option label="绿色食品" value="绿色食品" />
                  <el-option label="有机农产品" value="有机农产品" />
                     <el-option label="地理标志农产品" value="地理标志农产品" />
                        <el-option label="名特优新农产品" value="名特优新农产品" />
                        <el-option label="GAP认证农产品" value="GAP认证农产品" />
          </el-select>
        </el-form-item>
        <el-form-item label="农产品作物分类" prop="productClassify">
       <el-input v-model="form.productClassify" placeholder="请输入产品分类" />
        
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="认证时间" prop="certificateTime">
          <el-input v-model="form.certificateTime" placeholder="请输入认证时间" />
        </el-form-item>
        <el-form-item label="有效时间" prop="effectiveTime">
          <el-input v-model="form.effectiveTime" placeholder="请输入有效时间" />
        </el-form-item>
        <el-form-item label="证书编号" prop="certificateNumber">
          <el-input v-model="form.certificateNumber" placeholder="请输入证书编号" />
        </el-form-item>
        <el-form-item label="联系人" prop="linkedPerson">
          <el-input v-model="form.linkedPerson" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="linkedMobile">
          <el-input v-model="form.linkedMobile" placeholder="请输入联系电话" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCertificationInfo, getCertificationInfo, delCertificationInfo, addCertificationInfo, updateCertificationInfo, exportCertificationInfo } from "@/api/TProductCertificationInfo/certificationInfo";
import {
 getTree, listEnterpriseInfo,
} from "@/api/enterpriseInfo/EnterpriseInfo";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "CertificationInfo",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 农产品认证表格数据
      certificationInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      queryParams1: {
        pageNum: 1,
        pageSize: 1000,
        enterName: undefined,
        enterArea: undefined,
        plantId: undefined,
        orgId: undefined,
        sysProId: undefined,
        mainCertificeType: undefined,
        enterAddress: undefined,
        socialCreditCode: undefined,
        productOperationType: undefined,
        longitude: undefined,
        dimension: undefined,
        industry: undefined,
        enterIdcard: undefined,
        enterImg: undefined,
        enterLicenseImg: undefined,
        createDate: undefined,
      },
      // 行业
      options: [
        {
          value: "畜牧业",
          label: "畜牧业",
        },
        {
          value: "种植业",
          label: "种植业",
        },
        {
          value: "渔业",
          label: "渔业",
        },
        {
          value: "其他",
          label: "其他",
        },
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterName: undefined,
        enterArea: undefined,
        industry: undefined,
        enterId: undefined,
        certificateType: undefined,
        productClassify: undefined,
        productName: undefined,
        certificateTime: undefined,
        effectiveTime: undefined,
        certificateNumber: undefined,
        linkedPerson: undefined,
        linkedMobile: undefined
      },
      // 表单参数
      form: {},
      areaList: [],
      EnterpriseInfoList: [],
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getTreeList();
  },
  methods: {
    /** 查询农产品认证列表 */
    getList() {
      this.loading = true;
      listCertificationInfo(this.queryParams).then(response => {
        this.certificationInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTreeList() {
       getTree().then(response => {
	        this.areaList = this.handleTree(response.data, "id");
      });
    },
    normalizer(node) {
      console.log(JSON.stringify(node)+"11111111111111111111111111111111111111111111111111111")
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.label,
        children: node.children
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        enterId: undefined,
        certificateType: undefined,
        productClassify: undefined,
        productName: undefined,
        certificateTime: undefined,
        effectiveTime: undefined,
        certificateNumber: undefined,
        linkedPerson: undefined,
        linkedMobile: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.getList11();
      this.reset();
      this.open = true;
      this.title = "添加农产品认证";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.getList11();
      this.reset();
      const id = row.id || this.ids
      getCertificationInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改农产品认证";
      });
    },
    /** 查询种植主体管理列表 */
    getList11() {
      listEnterpriseInfo(this.queryParams1).then((response) => {
        this.EnterpriseInfoList = response.rows;
        this.loading = false;
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateCertificationInfo(this.form).then(response => {
              if (response.code == 0) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addCertificationInfo(this.form).then(response => {
              if (response.code == 0) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除农产品认证编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCertificationInfo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有农产品认证数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCertificationInfo(queryParams);
        }).then(response => {
          this.download(response.data);
        }).catch(function() {});
    }
  }
};
</script>
<style lang="scss" scoped>

.shuxing {
  width: 250px;
  float: left;
  .shuxing1{
    width: 150px !important;
    height: 35px;
  }
  
}
</style>